package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车商品信息
     * @param cart
     */
    @Override
    public void AddShoppingCart(ShoppingCartDTO cart) {
        //1.判断当前加入购物车的商品是否存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(cart,shoppingCart);
        //通过threadlocal获取用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //2.如果商品存在，则数量+1
        if(list != null && list.size() > 0) {
            ShoppingCart cart1 = list.get(0);
            cart1.setNumber(cart1.getNumber()+1);
            shoppingCartMapper.update(cart1);
        }
        //3.如果商品不存在，则添加到购物车，数量默认为1
        else{

            //判断添加到购物车的是菜品还是套餐
            Long dishId = cart.getDishId();
            if(dishId != null) {
                //添加到购物车是菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else {
                Setmeal setmeal = setmealMapper.getById(cart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);

        }

    }

    /**
     * 查询购物车商品信息
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCartList() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(userId).build();
        return shoppingCartMapper.list(shoppingCart);
    }

    /**
     * 清空购物车数据
     */
    @Override
    public void clean() {
        log.info("清空购物车");
        //根据当前用户id进行清空
        Long userId=BaseContext.getCurrentId();
        shoppingCartMapper.clean(userId);
    }

    /**
     * 减少购物车数据
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,cart);
        Long userId = BaseContext.getCurrentId();
        cart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.list(cart);
        //判断购物车数据
        if(list != null && list.size() > 0) {
            ShoppingCart cart1 = list.get(0);
            //判断购物车数量
            if(cart1.getNumber() > 1) {
                cart1.setNumber(cart1.getNumber()-1);
                shoppingCartMapper.update(cart1);
            }else if(cart1.getNumber() == 1) {
                shoppingCartMapper.deleteById(cart1.getId());
            }
        }

    }


}
